package com.wo2record.blog.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.wo2record.base.service.impl.BaseServiceAbstractImpl;
import com.wo2record.base.util.ModelUtil;
import com.wo2record.blog.model.Article;
import com.wo2record.blog.model.ArticleTag;
import com.wo2record.blog.service.ArticleService;
import com.wo2record.blog.service.ArticleTagService;

public class ArticleServiceImpl extends BaseServiceAbstractImpl<Article> implements ArticleService{

	@Autowired
	private ArticleTagService articleTagService;
	
	@Override
	public Article saveObject(Article article) throws Exception{
		createArticleTagsIfNesessary(article);
		super.saveObject(article);
		return article;
	}
	
	private void createArticleTagsIfNesessary(Article article) throws Exception{
		List<ArticleTag> articleTags = article.getArticleTags();
		if(!ModelUtil.isBlank(articleTags)){
			for(ArticleTag articleTag : articleTags){
				if(!articleTagService.isTagExist(articleTag)){
					articleTagService.saveObject(articleTag);
				}
			}
		}
	}
	
	@Override
	public Class<?> getObjectClass() {
		return Article.class;
	}

	@Override
	public void validateObjectBeforeSave(Article t) throws Exception {
		// TODO here should have some validation about saving an article
	}

}
